home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / AmiTCP30b2.lha / src / netlib / _write.c < prev    next >
C/C++ Source or Header  |  1994-04-04  |  2KB  |  119 lines

  1. RCS_ID_C="$Id: _write.c,v 1.4 1994/04/04 01:27:59 jraja Exp $";
  2. /*
  3.  * _write.c --- write() for both files and sockets.
  4.  *
  5.  * Author: jraja <Jarno.Rajahalme@hut.fi>
  6.  *
  7.  * This file is part of the AmiTCP/IP Network Support Library.
  8.  *
  9.  * Copyright © 1994 AmiTCP/IP Group, <amitcp-group@hut.fi>
  10.  *                  Helsinki University of Technology, Finland.
  11.  *                  All rights reserved.
  12.  *
  13.  * Created      : Tue Mar  8 22:43:57 1994 jraja
  14.  * Last modified: Wed Mar 30 10:17:32 1994 jraja
  15.  *
  16.  */
  17.  
  18. #include <ios1.h>
  19. #include <fcntl.h>
  20. #include <stdlib.h>
  21. #include <dos.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #include <dos/dos.h>
  25. #include <proto/dos.h>
  26.  
  27. #include <bsdsocket.h>
  28.  
  29. extern int __io2errno(long);
  30.  
  31. int
  32. __write(int fd, const void *buffer, unsigned int length)
  33. {
  34.   struct UFB *ufb;
  35.   int         count, totcount;
  36.   char       *ptr;
  37.  
  38.   /*
  39.    * Check for the break signals
  40.    */
  41.   __chkabort();
  42.   /*
  43.    * find the ufb *
  44.    */
  45.   if ((ufb = __chkufb(fd)) == NULL) {
  46.     errno = EINVAL;
  47.     return -1;
  48.   }
  49.   /*
  50.    * Check if write is allowed
  51.    */
  52.   if (!(ufb->ufbflg & UFB_WA)) {
  53.     _OSERR = ERROR_WRITE_PROTECTED;
  54.     errno = EIO;
  55.     return -1;
  56.   }
  57.  
  58.   /*
  59.    * Seek to end of the file if necessary
  60.    */
  61.   if (ufb->ufbflg & UFB_APP)
  62.     __lseek(fd, 0, 2);
  63.  
  64.   /*
  65.    * Check if translation is not needed
  66.    */
  67.   if (!(ufb->ufbflg & UFB_XLAT) ||
  68.       (ptr = memchr(buffer, 0x0A, length)) == NULL) {
  69.     if (ufb->ufbflg & UFB_SOCK) {
  70.       if ((count = send(fd, (char *)buffer, length, 0)) < 0)
  71.     return -1;
  72.     }
  73.     else {
  74.       if ((count = Write(ufb->ufbfh, (void *)buffer, length)) == -1)
  75.     goto osfail;
  76.     }
  77.     return count;
  78.   }
  79.  
  80.   totcount = length;
  81.  
  82.   /*
  83.    * Translate, ie., append CR before each LF
  84.    */
  85.   do {
  86.     count = ptr - (char *)buffer;
  87.     if (ufb->ufbflg & UFB_SOCK) {
  88.       if (send(fd, (char *)buffer, count, 0) < 0)
  89.     return -1;
  90.       if (send(fd, "\015"/* CR */, 1, 0) < 0)
  91.     return -1;
  92.     }
  93.     else {
  94.       if (Write(ufb->ufbfh, (void *)buffer, count) == -1)
  95.     goto osfail;
  96.       if (Write(ufb->ufbfh, "\015"/* CR */, 1) == -1)
  97.     goto osfail;
  98.     }
  99.     length -= count;
  100.     
  101.     buffer = ptr;
  102.   } while ((ptr = memchr((char *)buffer + 1, 0x0A, length)) != NULL);
  103.   
  104.   if (ufb->ufbflg & UFB_SOCK) {
  105.     if ((count = send(fd, (char *)buffer, length, 0)) < 0)
  106.       return -1;
  107.   }
  108.   else {
  109.     if (Write(ufb->ufbfh, (void *)buffer, length) == -1)
  110.       goto osfail;
  111.   }
  112.  
  113.   return totcount;
  114.  
  115. osfail:
  116.   errno = __io2errno(_OSERR = IoErr());
  117.   return -1;
  118. }
  119.